// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'

Vue.use(ElementUI);
import 'element-ui/lib/theme-chalk/index.css'
import {getRequest} from "./utils/api";
import {postRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";
import {postKeyValueRequest} from "./utils/api";
import {initMenu} from "./utils/menus";
import store from './store/index';

Vue.config.productionTip = false;
Vue.prototype.getRequest = getRequest;
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.postKeyValueRequest = postKeyValueRequest;

//配置一个全局前置守卫
router.beforeEach((to, from, next) => {
  //首先判断目标页面是不是Login，若是Login页面，则直接通过，因为Login页面不需要菜单数据
  if (to.name === 'Login') {
    next();
    return;
  }
  //获取store中保存的当前登录的用户数据，如果获取到的用户名为‘未登录’，则表示用户尚未登录，在用户尚未登录的情况下，
  //如果要跳转到某一个页面，则需要判断该页面是否要求登录后才能访问。若要求，则直接跳到登录页，并配置redirect参数。
  //如果用户已登录，则先执行initMenu方法初始化菜单数据，然后通过next()方法进入下一个页面
  let hr = store.state.currentHr;
  if (hr === undefined) {
    next({path: '/', query: {redirect: to.path}});
  }
  let name = hr.name;
  if (name === '未登录') {
    if (to.requireAuth || to.name === null) {
      next({path: '/', query: {redirect: to.path}});
    } else {
      next();
    }
  } else {
    initMenu(router, store);
    next();
  }
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: {App},
  template: '<App/>'
});
